package com.sxran.im.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.dao.ImUserGroupMapper;
import com.sxran.im.info.ImUserGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserGroupService {
    @Resource
    private ImUserGroupMapper imUserGroupMapper;

    /**
     * 用户加入的所有群组
     * @param userId
     * @return
     */
    public List<ImUserGroup> selectList(String userId){
        LambdaQueryWrapper<ImUserGroup> imUserGroupLambdaQueryWrapper = new LambdaQueryWrapper<ImUserGroup>().eq(ImUserGroup::getUid, userId);
        return imUserGroupMapper.selectList(imUserGroupLambdaQueryWrapper);
    }

    /**
     * 通过 组id 查询  组内的用户id。
     * @param groupId
     * @return
     */
    public List<String> getUserIds(String groupId){
        LambdaQueryWrapper<ImUserGroup> imUserGroupLambdaQueryWrapper = new LambdaQueryWrapper<ImUserGroup>().eq(ImUserGroup::getGid, groupId);
        imUserGroupLambdaQueryWrapper.select(ImUserGroup::getUid);
        return imUserGroupMapper.selectObjs(imUserGroupLambdaQueryWrapper).stream().map(Object::toString).collect(Collectors.toList());
    }

}
